package com.fatesgo.controller;

import com.fatesgo.mapper.TeacherMapper;
import com.fatesgo.pojo.Teacher;
import com.fatesgo.util.Response;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private HttpServletRequest request;
    @Autowired(required = false)
    private TeacherMapper teacherMapper;

    @GetMapping("/getTeacherInfo/{id}")
    public Response getTeacherInfoById(@PathVariable("id") int id) {
        Teacher teacher = teacherMapper.getTeacherInfoById(id);
        return Response.success("成功", teacher);
    }

    @PutMapping("/updateTeacherInfo")
    public Response updateTeacherInfo(@RequestBody Teacher teacher) {
        int row = teacherMapper.updateTeacherInfo(teacher);
        return Response.success("成功", row);
    }

    @PostMapping("/createTeacher")
    public Response createTeacher(@RequestBody Teacher teacher) {
        int row = teacherMapper.createTeacher(teacher);
        return Response.success("成功", row);
    }

    @DeleteMapping("/{id}")
    public Response deleteTeacher(@PathVariable("id") int id) {
        int row = teacherMapper.deleteTeacher(id);
        return Response.success("删除成功", row);
    }

    @GetMapping("/list")
    public Response list(Teacher teacher, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teachers = teacherMapper.selectAll(teacher);
        return Response.page(teachers, new PageInfo<>(teachers).getTotal());
    }
}
